const { expect } = require('chai');
require('./setup.js');

describe('Plugin Integration (No CSS)', () => {
    beforeEach(() => {
        // Clear DOM
        if (document.body) {
            document.body.innerHTML = '';
        }
    });

    it('should create button element when gitbook starts', (done) => {
        // Set up window.require mock for this test
        const originalRequire = global.window.require;
        global.window.require = (deps, callback) => {
            if (deps.includes('gitbook')) {
                const mockGitbook = {
                    events: {
                        bind: (event, handler) => {
                            if (event === 'start') {
                                // Simulate gitbook start event with config
                                setTimeout(() => {
                                    handler({}, {
                                        'atomgit-issue-feedback': {
                                            repo: 'test/repo',
                                            label: 'Test Report'
                                        }
                                    });
                                }, 100);
                            }
                        }
                    }
                };
                callback(mockGitbook);
            }
        };
        
        // Mock the CSS import by creating a simple plugin without CSS
        const mockPlugin = () => {
            global.window.require(['gitbook'], (gitbook) => {
                gitbook.events.bind('start', (e, pluginConfig) => {
                    const config = pluginConfig['atomgit-issue-feedback'];
                    
                    if (!config || !config.repo) {
                        console.warn('AtomGit Issue Feedback: Repository configuration is missing');
                        return;
                    }

                    const reportElement = document.createElement('button');
                    reportElement.textContent = config.label || 'Bug Report';
                    reportElement.className = 'gitbook-plugin-AtomGit-issue-feedback';
                    reportElement.setAttribute('aria-label', 'Report an issue on AtomGit');
                    reportElement.setAttribute('title', 'Click to report an issue about this page');
                    
                    document.body.appendChild(reportElement);
                });
            });
        };
        
        // Execute the mock plugin
        mockPlugin();
        
        // Wait for gitbook start event to fire
        setTimeout(() => {
            const button = document.querySelector('.gitbook-plugin-AtomGit-issue-feedback');
            expect(button).to.not.be.null;
            expect(button.textContent).to.equal('Test Report');
            expect(button.getAttribute('aria-label')).to.equal('Report an issue on AtomGit');
            // Restore original require
            global.window.require = originalRequire;
            done();
        }, 150);
    });

    it('should handle missing configuration gracefully', (done) => {
        // Override window.require to provide no config
        const originalRequire = global.window.require;
        global.window.require = (deps, callback) => {
            if (deps.includes('gitbook')) {
                const mockGitbook = {
                    events: {
                        bind: (event, handler) => {
                            if (event === 'start') {
                                // Simulate gitbook start event with no config
                                setTimeout(() => {
                                    handler({}, {}); // Empty pluginConfig
                                }, 100);
                            }
                        }
                    }
                };
                callback(mockGitbook);
            }
        };
        
        // Mock the CSS import by creating a simple plugin without CSS
        // Access global.window directly
        const mockPlugin = () => {
            global.window.require(['gitbook'], (gitbook) => {
                gitbook.events.bind('start', (e, pluginConfig) => {
                    const config = pluginConfig['atomgit-issue-feedback'];
                    
                    if (!config || !config.repo) {
                        console.warn('AtomGit Issue Feedback: Repository configuration is missing');
                        return;
                    }

                    const reportElement = document.createElement('button');
                    reportElement.textContent = config.label || 'Bug Report';
                    reportElement.className = 'gitbook-plugin-AtomGit-issue-feedback';
                    reportElement.setAttribute('aria-label', 'Report an issue on AtomGit');
                    reportElement.setAttribute('title', 'Click to report an issue about this page');
                    
                    document.body.appendChild(reportElement);
                });
            });
        };
        
        // Execute the mock plugin
        mockPlugin();
        
        // Simulate gitbook start with no config
        setTimeout(() => {
            const button = document.querySelector('.gitbook-plugin-AtomGit-issue-feedback');
            expect(button).to.be.null;
            // Restore original require
            global.window.require = originalRequire;
            done();
        }, 100);
    });
});
